package com.syni.mdd.yhd.common.all.controller.protect.order.v3;

/**
 * @className BmsBusinessController
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/15 14:25
 * @VERSION 1.0
 **/

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.utils.WebUtils;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderStatusService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.redis.utils.RedisTemplateUtils;;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/v3/dxOrder")
@Log4j2
public class BmsDxOrderController_V3 {

    @Autowired
    BmsDxOrderService bmsDxOrderService;

    @Autowired
    BmsDxOrderStatusService bmsDxOrderStatusService;

    @CheckParams(checkAuth = true,params = {"businessId","orderType"})
    @PostMapping("/commitOrder")
    public Map<String,Object> commitOrder(@RequestBody ParamsMap paramsMap){
        try {
            return bmsDxOrderService.orderCommit_V3(paramsMap);
        }finally {
            //释放分布式锁
            if(paramsMap.containsKey("isAddLock")){
                String lockKey = paramsMap.get("isAddLock").toString();
                RedisTemplateUtils.releaseLock(lockKey);
            }
            if(paramsMap.containsKey("lockBusinessId")){
                RedisTemplateUtils.releaseLock(paramsMap.get("lockBusinessId").toString());
            }
            if(paramsMap.containsKey("userBuyGoodLock")){
                String userBuyGoodLock = paramsMap.get("userBuyGoodLock").toString().split("&")[0];
                String uid = paramsMap.get("userBuyGoodLock").toString().split("&")[1];
                RedisTemplateUtils.releaseLock(userBuyGoodLock,uid);
            }
        }
    }

    /**
     * @Description 用户端单条订单退款详情
     * @Author: cfx
     * @Date: 2019/8/2
     */
    @PostMapping("getRefundOrderDetail")
    @CheckParams(checkAuth = true,params = {"orderId","groupBuyUseId"})
    public Map<String,Object> getRefundOrderDetail(@RequestBody ParamsMap paramsMap){
        return bmsDxOrderService.getRefundOrderDetail_v3(paramsMap);
    }

    /**
     * @Description 用户端退款接口
     * @Author: cfx
     * @Date: 2019/8/2
     */
    @PostMapping("/refundOrder")
    @CheckParams(checkAuth = true,params = {"orderId","groupBuyUseIds"})
    public Map<String,Object> refundOrder(@RequestBody ParamsMap paramsMap) {
        Map<String,Object> resultMap;
        String lockValue = WebUtils.getUUId();
        String lockKey = Constants.REDIS_ORDER_REFUND + paramsMap.getString("orderId");
        try {
            boolean addLockResult = RedisTemplateUtils.addLockCanWait(lockKey,lockValue,5);
            if(!addLockResult){
                return ResultMapHelper.result(ResultCode.CODE_ORDER_REFUND_TOO_LOGN,"退款超时,请重试");
            }
            resultMap =  bmsDxOrderService.refundOrder_v3(paramsMap);
        }catch (Exception e){
            log.error("refundOrder error",e);
            resultMap = ResultMapHelper.result(ResultCode.CODE_ORDER_REFUND_TOO_LOGN,"退款超时,请重试");
        }finally {
            RedisTemplateUtils.releaseLock(lockKey,lockValue);
        }
        return resultMap;
    }


    /**
     * @Description 用户端退款接口
     * @Author: cfx
     * @Date: 2019/8/2
     */
    @PostMapping("checkOrderIsExist")
    @CheckParams(checkAuth = true,params = {"orderId"})
    public Map<String,Object> checkOrderIsExist(@RequestBody ParamsMap paramsMap) {
        Integer orderId = paramsMap.getInteger("orderId");
        return bmsDxOrderService.checkOrderIsExist(orderId);
    }
}
